﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Results.Models
{
    /// <summary>
    /// A golf tournament
    /// </summary>
    public class TournamentModel : IComparable
    {
        /// <summary>
        /// Data
        /// </summary>
        public String Name { get; set; }
        public String Course { get; set; }
        public String Location { get; set; }
        public DateTime From { get; set; }
        public DateTime To { get; set; }
        public String Website { get; set; }
        public String Season { get; set; }
        public String Weather { get; set; }

        /// <summary>
        /// The players and their scores that played in this tournament
        /// </summary>
        public List<ResultModel> Results { get; private set; }

        public TournamentModel()
        {
            Results = new List<ResultModel>();
        }

        /// <summary>
        /// Does this tournament contain results (scores) from people who belong to the specified group and was this tournament conducted in the specified season?
        /// </summary>
        /// <param name="selectedGroup">e.g. "Men 14-16"</param>
        /// <param name="selectedSeason">e.g. "2010"</param>
        /// <returns></returns>
        public Boolean Contains(string selectedGroup, string selectedSeason)
        {
            if (Season == selectedSeason)
            {
                return Results.Count(resultModel => resultModel.Group == selectedGroup) > 0;
            }

            return false;
        }

        #region IComparable Members

        /// <summary>
        /// Sort tournaments by date
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            var otherTournamentModel = obj as TournamentModel;
            if (otherTournamentModel == null)
            {
                return 0;
            }

            return From.CompareTo(otherTournamentModel.From) * -1;
        }

        #endregion
    }
}
